package com.zzjson.io.bio;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * <p>****************************************************************************</p>
 * <p><b>Copyright © 2010-2019 shuncom team All Rights Reserved<b></p>
 * <ul style="margin:15px;">
 * <li>Description : cn.zzjson.io.bio</li>
 * <li>Version     : 1.0.0</li>
 * <li>Creation    : 2019年03月26日</li>
 * <li>@author     : zzy0_0</li>
 * </ul>
 * <p>****************************************************************************</p>
 */
public class BIOServer1 {
    ServerSocket socket;
    int port = 8080;

    public BIOServer1(int port) {
        try {
            this.port = port;
            socket = new ServerSocket(port);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        new BIOServer1(8080).listen();

    }

    private void listen() throws IOException {
        while (true) {
            Socket client = socket.accept();
            InputStream inputStream = client.getInputStream();
            byte[] bytes = new byte[10];
            int i = 0;
            StringBuilder sb = new StringBuilder();
            i = inputStream.read(bytes);
            while (i > 0) {
                sb.append(new String(bytes, 0, i));
                i = inputStream.read(bytes);
            }
            System.out.println("received Msg:" + sb.toString());

        }
    }
}

class BIOClient {
    private static final int count = 800;

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(count);
        CountDownLatch countDownLatch = new CountDownLatch(count);
        for (int i = 0; i < count; i++) {
            int finalI = i;
            executorService.submit(() -> {
                Socket socket = null;
                sendMessage(socket, finalI);
                countDownLatch.countDown();
            });
        }
        try {
            countDownLatch.await();
            executorService.shutdown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void sendMessage(Socket socket, int i) {
        OutputStream outputStream = null;
        try {
            socket = new Socket("localhost", 8080);
            outputStream = socket.getOutputStream();
            String s = i + "hello -hellohello hellohello------------------------------------------hellohello------------------------------------------hello";
            outputStream.write(s.getBytes());
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (socket != null) {
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
